#include "form_vcplay.h"
#include "ui_form_vcplay.h"
#include <QSettings>
#include <QMessageBox>

// 全局配置的获取和设置
QString getInfor(QString key, QString default_return=""){
    QSettings cfg(QSettings::IniFormat, QSettings::UserScope, "QtScrcpy_form_vcplay");
    QString value=cfg.value(key, "").toString();
    value = QString::fromStdString(value.toStdString());
    if(value.isEmpty()) return default_return;
    return value;
}
void setInfor(QString key, QString data){
    QSettings cfg(QSettings::IniFormat,QSettings::UserScope, "QtScrcpy_form_vcplay");
    cfg.setValue(key, QString::fromStdString(data.toStdString()));
}

Form_Vcplay::Form_Vcplay(QWidget *parent) : QWidget(parent), ui(new Ui::Form_Vcplay)
{
    ui->setupUi(this);
    if(!getInfor("text1").isEmpty()){
        ui->lineEdit->setText(getInfor("text1"));
    }
    if(!getInfor("text2").isEmpty()){
        ui->lineEdit_2->setText(getInfor("text2"));
    }
}

Form_Vcplay::~Form_Vcplay()
{
    delete ui;
}

void Form_Vcplay::on_pushButton_toggled(bool checked)
{
    if(checked){
        callThread.SetIpAndPort(ui->lineEdit->text(), ui->lineEdit_2->text());
        ui->pushButton->setText("停止线程");
    }else{
        ui->pushButton->setText("启动线程");
    }
    callThread.pause(!checked);
}

void Form_Vcplay::on_lineEdit_textChanged(const QString &arg1)
{
    setInfor("text1", arg1);
}

void Form_Vcplay::on_lineEdit_2_textChanged(const QString &arg1)
{
    setInfor("text2", arg1);
}

void Form_Vcplay::on_pushButton_2_clicked()
{
    if(!remoteCall.releaseFile()){
        QMessageBox::warning(this, "错误", "释放文件失败!");
    }else{
        QMessageBox::information(this, "成功", "文件已释放到/data/local/tmp/remote_call");
    }
}

